/**
 * @description:
 * @author: asd
 * @time: 2022/10/7 0:14
 */
public class demo1 {
    //sleep,join
    public static void main(String[] args) throws Exception {
        Thread t1 = new WorkThread("work1");
        Thread t2 = new WorkThread("work2");
        t1.start();
        t1.join();
        t2.start();
        //t2.join();
        System.out.println("当前主线程结束");
        /*Thread t3 = new YieldClass("work3",2);
        Thread t4 = new YieldClass("work4",1);
        t3.start();
        t4.start();*/
    }
    //工作线程
    static class WorkThread extends Thread{

        public WorkThread(String name){
            super(name);
        }

        @Override
        public void run() {
            try {
                sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("the current thread is " + getName());
        }
    }
    static class YieldClass extends Thread {
        public int mIndex;
        public YieldClass(String name, int index) {
            super(name);
            this.mIndex = index;
        }
        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                System.out.println(Thread.currentThread().getName() + "---" + i);
                if (i == mIndex) {
                    this.yield ();
                    try {
                        sleep(2000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    //break;
                }
            }
        }
    }
}
